package cz.hanis.android.geography.activities;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import cz.hanis.android.geography.AboutDialog;
import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.HighscoreHandler;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestManager;
import cz.hanis.android.geography.TestSpecification;
import cz.hanis.android.geography.models.Continent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class TestResultActivity extends SherlockActivity {

	public static final String CORRECT_ANSWER_KEY = "coorect_answer_key";
	public static final String INCORRECT_ANSWER_KEY = "incoorect_answer_key";
	
	public static final String NEW_HIGHSCORE = "new_highscore_key";
	
	private Animation mScoreAnimation;
	private Animation mNewHighscoreAnimation;
	
	private TestSpecification testSpec;
	
	private boolean newHighscore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_result);		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mScoreAnimation =  AnimationUtils.loadAnimation(this, R.anim.score);
		mNewHighscoreAnimation =  AnimationUtils.loadAnimation(this, R.anim.new_highscore);		
		HighscoreHandler highscoreHandler = GeographyApplication.getInstance().getHighScoreHandler();				
		
		testSpec = (TestSpecification) getIntent().getParcelableExtra(TestSpecification.KEY);
		Log.d("SPEC-Result", testSpec.toString());
		int correct = getIntent().getIntExtra(TestResultActivity.CORRECT_ANSWER_KEY, -1);						
		int incorrect = getIntent().getIntExtra(TestResultActivity.INCORRECT_ANSWER_KEY, -1);				
		int score = 0;
		if(testSpec.getTestTypeId() == TestManager.TEST_TYPE_LIFE) {
			score = correct;
		} else if(testSpec.getTestTypeId() == TestManager.TEST_TYPE_TIME) {
			score = correct - incorrect;
			TextView tvScoreTitle = (TextView) findViewById(R.id.tr_score_title);
			tvScoreTitle.setText(tvScoreTitle.getText() + " " + getResources().getString(R.string.tv_result_score_title));
		}
		int highscore = highscoreHandler.getHighScoreAndSetIfGreater(testSpec.getTestId(), testSpec.getContinent(), testSpec.getTestTypeId(), score);		
		if(score > highscore || (savedInstanceState != null && savedInstanceState.getBoolean(NEW_HIGHSCORE))) {
			highscore = score;
			newHighscore = true;
			TextView tvNewHighscore = (TextView) findViewById(R.id.tr_new_highscore);
			tvNewHighscore.setVisibility(View.VISIBLE);
		} else {
			newHighscore = false;
		}
		
		
		TextView tvTestType = (TextView) findViewById(R.id.tr_test_type);
		tvTestType.setText(GeographyApplication.getInstance().getTestManager().getTestTypeName(testSpec.getTestTypeId()));
												
		TextView tvHighscore = (TextView) findViewById(R.id.tr_highscore);
		tvHighscore.setText(String.valueOf(highscore));		
	
		TextView tvTestName = (TextView) findViewById(R.id.tr_test);
		tvTestName.setText(GeographyApplication.getInstance().getTestManager().getTestById(testSpec.getTestId()).getName());		
		
		String continent = Continent.getNamyById(testSpec.getContinent());
		TextView tvContinent = (TextView) findViewById(R.id.tr_continent);
		tvContinent.setText(continent);
		
		TextView tvScore = (TextView) findViewById(R.id.tr_score);
		tvScore.setText(String.valueOf(score));
		tvScore.startAnimation(mScoreAnimation);			
	}

	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean(NEW_HIGHSCORE, newHighscore);
	}	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.result_menu, menu);;
        return super.onCreateOptionsMenu(menu);
    }	
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {		
            case android.R.id.home:
                Intent parentActivityIntent = new Intent(this, TestChooserActivity.class);
                parentActivityIntent.addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(parentActivityIntent);
                finish();
                return true;
            case R.id.rm_menu_again:
				String testClass = GeographyApplication.getInstance().getTestManager().getTestById(testSpec.getTestId()).getClassName();
				if(testSpec.getTestTypeId() == TestManager.TEST_TYPE_TIME) {
					testClass += "TIME";
				}
				Intent intent = new Intent(testClass);
				intent.putExtra(TestSpecification.KEY, testSpec);
				startActivity(intent); 
				return true;
            case R.id.rm_menu_statistics:
    			Intent highscoreIntent = new Intent(TestResultActivity.this, HighScoreTableActivity.class);
    			highscoreIntent.putExtra(HighScoreTableActivity.FROM_ACTIVITY, HighScoreTableActivity.FROM_RESULT_ACTIVITY);
    			startActivity(highscoreIntent);
    			return true;    
            case R.id.rm_menu_about:
    			AboutDialog about = new AboutDialog(this);
    			about.setTitle(getResources().getString(R.string.menu_welcome_about));
    			about.show();
    			return true;    			
            default:    			
                return super.onOptionsItemSelected(item);
        }
    } 	

	
}